package com.cmict.data.infra.dal.mysql.consultcategory;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cmict.data.framework.mybatis.core.mapper.BaseMapperX;
import com.cmict.data.infra.dal.dataobject.consultcategory.ConsultCategoryDO;
import org.apache.ibatis.annotations.Mapper;
import com.cmict.data.infra.controller.admin.consultcategory.vo.*;
import org.apache.ibatis.annotations.Update;

/**
 * 问答分类 Mapper
 *
 * @author 系统管理员
 */
@Mapper
public interface ConsultCategoryMapper extends BaseMapperX<ConsultCategoryDO> {

    default PageResult<ConsultCategoryDO> selectPage(ConsultCategoryPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ConsultCategoryDO>()
                .likeIfPresent(ConsultCategoryDO::getCategoryName, reqVO.getCategoryName())
                .orderByAsc(ConsultCategoryDO::getCategoryOrder)
                .orderByDesc(ConsultCategoryDO::getCreateTime)
        );
    }

    default List<ConsultCategoryDO> selectList(ConsultCategoryExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ConsultCategoryDO>()
                .likeIfPresent(ConsultCategoryDO::getCategoryName, reqVO.getCategoryName())
                .eqIfPresent(ConsultCategoryDO::getCategoryOrder, reqVO.getCategoryOrder())
                .eqIfPresent(ConsultCategoryDO::getCategoryRemarks, reqVO.getCategoryRemarks())
                .betweenIfPresent(ConsultCategoryDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(ConsultCategoryDO::getId));
    }

    default  List<ConsultCategoryDO> getConsultCategoryList(ConsultCategoryBaseVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ConsultCategoryDO>()
                .likeIfPresent(ConsultCategoryDO::getCategoryName, reqVO.getCategoryName())
                        .orderByAsc(ConsultCategoryDO::getCategoryOrder)
                        .orderByDesc(ConsultCategoryDO::getCreateTime)
                );
    }

    default ConsultCategoryDO selectOne(Integer categoryOrder){
        return selectOne(new LambdaQueryWrapperX<ConsultCategoryDO>().eq(ConsultCategoryDO::getCategoryOrder,categoryOrder));
    }

    @Update("update sys_consult_category set category_order = category_order + 1 WHERE category_order >= #{categoryOrder} and deleted = 0")
    Boolean updateCategoryOrder(Integer categoryOrder);

    @Update("update sys_consult_category set category_order = category_order - 1 WHERE category_order > #{arg0} AND category_order <= #{arg1} AND deleted = 0")
    Boolean littleToBig(Integer yuanCategoryOrder, Integer beiCategoryOrder);

    @Update("update sys_consult_category set category_order = category_order + 1 WHERE category_order >= #{arg1} AND category_order < #{arg0} AND deleted = 0")
    Boolean bigToLittle(Integer yuanCategoryOrder, Integer beiCategoryOrder);
}
